//
//  BLLiveListViewController.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/6/10.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit
import MJRefresh

class BLSubListViewController: UICollectionViewController {
    
    let subListCellID = "subListCellID"
    var tagID : NSNumber?
    var subListArray = [BLDouyuRoomDetalModal]()
    var isSetTitle = false
    //导航栏标题
    var navTitleText = ""{
        didSet{
            navigationItem.title = navTitleText
        }
    }
    
    class func subListVC() -> BLSubListViewController {
        let subListVC = BLSubListViewController(collectionViewLayout: BLSubListFlowLayout())
        return subListVC
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView!.backgroundColor = UIColor.whiteColor()
        getSubListData(0)
        print(tagID)
        collectionView?.registerNib(UINib.init(nibName: String(BLSubListCell), bundle: nil), forCellWithReuseIdentifier: subListCellID)
        
        //设置下拉刷新
        collectionView?.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.getRefreshSubListData()
        })
    }
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return subListArray.count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(subListCellID, forIndexPath: indexPath) as! BLSubListCell
        cell.subListItem = subListArray[indexPath.item]
        if indexPath.item == (subListArray.count - 1) {
            getSubListData(subListArray.count)
        }
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let roomVC = BLRoomViewController()
        let item = subListArray[indexPath.item]
        roomVC.roomID = item.room_id!
        navigationController?.pushViewController(roomVC, animated: true)
    }
    
}


extension BLSubListViewController{
    func getSubListData(offset : Int) {
        guard tagID != nil else{
            return
        }
        BLGetDouyuData.shareInstance.getSubLiveData(tagID!, limit: 20, offset: offset) { (dataArray) in
            
            for item in dataArray!{
                self.subListArray.append(item)
            }
            self.collectionView!.reloadData()
        }
    }
    
    func getRefreshSubListData() {
        
        BLGetDouyuData.shareInstance.getSubLiveData(tagID!, limit: 20, offset: 20) { (dataArray) in
            
            self.subListArray = dataArray!
            
            if self.isSetTitle == false{
                self.isSetTitle = true
                self.navigationItem.title = self.subListArray.first?.game_name
            }
            
            //移除下拉刷新
            self.collectionView?.mj_header.endRefreshing()
            
            self.collectionView!.reloadData()
        }

    }
}